mui.init();
var wxChannel = null;
var aliChannel = null;
var channel = null;
mui.init({
	swipeBack: true
});

//验证手机号
function isPhoneNo(phone) {
	var pattern = /^1[34578]\d{9}$/;
	return pattern.test(phone);
};

var num = $('.proNum').text().substring(1, 2);
var sp = parseInt($('.singlePrice').text());
var pap = parseInt($('.proAllPrice').text());
var fp = parseInt($('.freightPrice').text());
var total = '￥' + (sp * num + fp);
$('.totalPrice').text(total);

mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	order_list = new Vue({
		el: '#order_detail',
		data: {
			title: '',
			image: '',
			tab_sku: '',
			nickname: '',
			sku_id: '',
			buy_num: '1',
			mobilenum: '',
			tab_price: '',
			shipping_price: '',
			address: '',
			payway: 'wxpay',
			product_id: '',
			loginSta: ''
		},
		created: function() {
			this.get_data();
		},
		mounted() {
			this.updateStatus();
			this.getUserInfo();
		},
		computed: {
			totalPrice: function() {
				return Number(this.tab_price) * Number(this.buy_num) + Number(this.shipping_price);
			},
			productPrice: function() {
				return Number(this.tab_price) * Number(this.buy_num);
			}
		},
		methods: {
			updateStatus: function() {
				showWatting();
				var api_host = plus.storage.getItem('HOST_URL');
				var self = this;
				mui.ajax({
					url: api_host + '/api/linxuan/user/checkLoginStatus',
					dataType: 'json',
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					timeout: 3000,
					type: 'get',
					success: function(msg) {
						if (msg.code == 1) {
							self.loginSta = 1;
							self.user_detail = msg.data;
						} else {
							self.loginSta = 0;
						}
					},
					error: function(xhr, type, errorThrown) {
						mui.toast('登录状态请求失败请重试', {
							duration: '300'
						});
					},
					complete: function() {
						plus.nativeUI.closeWaiting();
					}
				});
			},
			
			getUserInfo: function() {
				showWatting();
				var api_host = plus.storage.getItem('HOST_URL');
				var self = this;
				mui.ajax({
					url: api_host + '/api/user/user/userAddress',
					dataType: 'json',
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					timeout: 3000,
					type: 'get',
					success: function(msg) {
						if (msg.code == 1) {
							console.log(JSON.stringify(msg.data));
							order_list.nickname = msg.data.nickname;
							order_list.mobilenum = msg.data.mobile;
							order_list.address = msg.data.address;
						}
					},
					error: function(xhr, type, errorThrown) {
						mui.toast('登录状态请求失败请重试', {
							duration: '300'
						});
					},
					complete: function() {
						plus.nativeUI.closeWaiting();
					}
				});
			},

			get_data: function() {
				this.title = self.title;
				this.image = self.image;
				this.sku_id = self.sku_id;
				this.tab_sku = self.tab_sku;
				this.buy_num = self.buy_num;
				this.tab_price = self.tab_price;
				this.shipping_price = self.shipping_price;
				this.product_id = self.product_id;
				this.address = self.address;
				$('.showAddr').text(self.address);
				/*获取当前用户是否有默认收货地址*/

			},

			addAddress: function() {
				$('.popMask').show();
				$('.popAddress').animate({
					'bottom': '0%'
				}, 300).show();
			},

			colsePay: function() {
				$('.popMask').hide();
				$('.popAddress').animate({
					'bottom': '-100%'
				}, 300).hide(300).stop(true, true);

				$('.popPay').animate({
					'bottom': '-100%'
				}, 300).show();
			},
			saveBtn: function() {
				if ($.trim($('.nameInput').val()) == '') {
					mui.toast('请输入姓名');
					$('.nameInput').focus();
					return false;
				}
				// 判断手机号码
				if ($.trim($('.telInput').val()).length == 0) {
					mui.toast('请输入手机号');
					$('.telInput').focus();
					return false;
				} else if (!isPhoneNo($.trim($('.telInput').val()))) {
					mui.toast('请输入有效的手机号码');
					$('.telInput').focus();
					return false;
				}

				if ($('#showAddress').text() == '请选择地区') {
					mui.toast('请选择地区');
					return false;
				}

				if ($('.addrInput').val() == '') {
					mui.toast('请完善收货地址');
					return false;
				}

				$('.popMask').hide();
				$('.popAddress').animate({
					'bottom': '-100%'
				}, 300).hide(300);

				$('.addAddress').hide();
				$('.showAddrInfo').show();

				$('.showName').text($('.nameInput').val());
				$('.showTel').text($('.telInput').val());

				if ($('#showAddress').text() == '请选择地区') {
					$('#showAddress').text('');
					mui.toast('请选择地区');
					return false;
				} else {
					AddrVal = $('#showAddress').text() + $('.addrInput').val();
					order_list.address = AddrVal;
					$('.showAddr').text(AddrVal);
					order_list.nickname = $('.nameInput').val();
					order_list.mobilenum = $('.telInput').val();

					var api_host = plus.storage.getItem('HOST_URL');

					mui.ajax({
						url: api_host + '/api/shop/address/add',
						dataType: 'json',
						data: {
							'address': order_list.address,
							'nickname': order_list.nickname,
							'mobile': order_list.mobilenum
						},
						headers: {
							'User-Token': plus.storage.getItem('UserToken')
						},
						timeout: 3000,
						type: 'post',
						async: true,
						success: function(data) {
							mui.toast(data.msg, {
								duration: '1000'
							});
						},
						error: function(xhr, type, errorThrown) {
							console.log('系统错误!');
						}
					})
				}
			},
			showAddrInfo: function() {
				$('.popMask').show();
				$('.popAddress').animate({
					'bottom': '0%'
				}, 300).show();
				$('.pop_tt').find('h4').text('修改收货地址');
			},
			subBtn: function() {
				if (!(order_list.address)) {
					mui.toast('请输入收货地址');
					return;
				}
				$('.popMask').show();
				$('.popPay').animate({
					'bottom': '0%'
				}, 300).show();
			},
			pay_way: function(e) {
				order_list.payway = e;
			},
			showAddress: function() {
				var adressStr = '';
				//显示几层，我们要显示省、市、区，所以我们这里写三层，如果不写默认是一层
				var addressPicker = new mui.PopPicker({
					layer: 3
				});
				addressPicker.setData(cityData3);
				//city.data-3.js中的数据

				addressPicker.show(function(selectItems) {
					//将选择的省、市、区显示到屏幕上
					for (var i = 0; i < selectItems.length; i++) {
						adressStr += selectItems[i].text;
					}
					document.getElementById('showAddress').innerHTML = adressStr;
				});
			},
			openPay: function() {
				if(this.payway == 'wxpay'){
					mui.toast('正在启动微信……');
				}else{
					mui.toast('正在启动支付宝……');
				}
				this.pay(order_list.payway)
			},
			pay: function(id) {
				
				if(typeof order_list.nickname == "undefined" || order_list.nickname == null || order_list.nickname == ""){
					mui.toast('请输入收货人');
					return;
				}
				if(typeof order_list.address == "undefined" || order_list.address == null || order_list.address == "")
				{
					mui.toast('请输入收货地址');
					return;
				}
				
				if(typeof order_list.mobilenum == "undefined" || order_list.mobilenum == null || order_list.mobilenum == "")
				{
					mui.toast('请输入手机号');
					return;
				}
				

				var goodsInfo = {
					'id': this.sku_id,
					'product_id': this.product_id,
					'nickname': order_list.nickname,
					'address': order_list.address,
					'mobile': order_list.mobilenum
				};
				new pay().start('shop', this.payway, this.buy_num, goodsInfo);
			},

			/*paymentSuccess:function(){
				var api_host = plus.storage.getItem('HOST_URL');
				var userInfo = JSON.parse(plus.storage.getItem('userInfo'));
				
			    mui.ajax({
			        url:api_host+'/api/pay/wxpay/update_order_status',
			        dataType:'json',
			        data:{'id':this.sku_id,'type':'shop','userToken':plus.storage.getItem('user_token'),'mobile':userInfo.mobilenum},
			        type:'post',
			        async: true,
			        timeout:1000,
			        success:function(data){
			            mui.toast(data.msg,{ duration:'1000', type:'div'});
			        },
			        error:function(xhr,type,errorThrown){
			            console.log('系统错误！')
			        }
			    });
			}*/



		}
	})
})
